#########################
#   Author : Leon yu    #
#   Date : 2025/06/12   #
#   Id : SM2772         #
#########################
from apiRespsAnalyze import *

# 数据解析
def queryStoreSalesReportResult(startTime, endTime):
    result = dict()
    # 总订单数
    result['ORDER_COUNT'] = 0
    # 总营业收入
    result['TURNOVER'] = 0
    # 优惠总数
    result['DISCOUNT_AMOUNT'] = 0
    # 总销售额
    result['SALES_AMOUNT'] = 0
    # 收款方式
    result['payMethodReport'] = list()
    # 收款方式-总计
    result['payTotalAmount'] = 0
    # 订单渠道统计
    result['payChannelReport'] = list()
    # 订单渠道统计-共计
    result['payChannelAmount'] = 0
    
    queryStoreSalesReport = analyzeQueryStoreSalesReport(startTime=startTime, endTime=endTime)
    if queryStoreSalesReport:
        for item in queryStoreSalesReport['statisticReport']:
            if item['key'] not in ['ORDER_COUNT', 'TURNOVER', 'DISCOUNT_AMOUNT', 'SALES_AMOUNT']:
                continue
            if item['key'] == 'ORDER_COUNT':
                result[item['key']] = abs(item['value'])
            else:
                result[item['key']] = abs(item['value']['amount'])
        if len(queryStoreSalesReport['payMethodReport']) > 0:
            for item in queryStoreSalesReport['payMethodReport']:
                itemDict = {}
                itemDict['payMethodName'] = item['payMethodName']
                itemDict['amount'] = item['amount']['amount']
                result['payMethodReport'].append(itemDict)
        result['payTotalAmount'] = queryStoreSalesReport['payTotalAmount']['amount']
        if len(queryStoreSalesReport['payChannelReport']) > 0:
            for item in queryStoreSalesReport['payChannelReport']:
                itemDict = {}
                itemDict['subPlatform'] = item['subPlatform']
                itemDict['orderCount'] = item['orderCount']
                itemDict['saleAmount'] = item['saleAmount']['amount']
                result['payChannelReport'].append(itemDict)
            result['payChannelReportOrderCount'] = {i['subPlatform']:i['orderCount'] for i in result['payChannelReport']}
            del result['payChannelReport']
            
        result['payChannelAmount'] = queryStoreSalesReport['payChannelAmount']['amount']
    
    return result
     
     


# startTime=1717434000000
# endTime=1717520399000
# print(queryStoreSalesReportResult(startTime, endTime))